<?php

namespace App\Controller\Admin;

use App\Model\AuthUser;
use App\Service\UserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

/**
 * @Controller(prefix="/admin/auth")
 */
class AdminAuthController extends AdminBaseController
{


    /**
     * @Inject()
     * @var UserService
     */
    public $userService;


    /**
     * 后台登录
     * @RequestMapping(path="login")
     * @return ResponseInterface|null
     */
    public function login()
    {
        $params = $this->request->inputs(['username', 'password']);
        $this->validate($params, [
            'username' => "required",
            'password' => 'required',
        ]);

        $adminUser = AuthUser::where(['username' => $params['username'],'status'=>1])->first();
        if (is_null($adminUser)) {
            return $this->response->adminFail();
        }
        if (!password_verify($params['password'], $adminUser->password)) {
            return $this->response->adminFail();
        }

        return $this->userService->adminUserLogin($adminUser['id']);


    }

}